﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Chaos.network
{
    class gameclient
    {
        public tcpconnection m_connection;

        public bool Running { get; set; }

        public void shutdown()
        {
            close();
        }
           
        public void close()
        {
            if (m_connection != null)
            {
                m_connection.abort();
            }
            Running = false;
        }

        public void open(String IP, int Port)
        {
            IPAddress ipAddr = IPAddress.Parse(IP);
            IPEndPoint ipendpoint = new IPEndPoint(ipAddr, Port);
            open(ipendpoint);
        }

        public void open(IPEndPoint ipendpoint)
        {         
            try
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect(ipendpoint);
                m_connection = new tcpconnection(tcpClient, ipendpoint);
                m_connection.OnNetworkData += new tcpconnection.OnNetworkDataDelegate(Connection_OnNetworkData);
                m_connection.OnNetworkConnected += new connection.OnNetworkConnectedDelegate(Connection_OnNetworkConnected);
                m_connection.OnNetworkDisConnected += new connection.OnNetworkDisConnectedDelegate(Connection_OnNetworkDisConnected);
                m_connection.start();
                Running = true;
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        void Connection_OnNetworkDisConnected(object sender, connection.OnNetworkDisConnectedEvent e)
        {
            view.manager m = System.Windows.Application.Current.FindResource("view_manager") as view.manager;
            if (m != null)
            {
                MainWindow w = m.Controls["main_window"] as MainWindow;
                if (w != null)
                {
                    w.ScreenGrid_show(w.ConnectScreen);
                }
            }              
        }

        void Connection_OnNetworkConnected(object sender, connection.OnNetworkConnectedEvent e)
        {
            view.manager m = System.Windows.Application.Current.FindResource("view_manager") as view.manager;
            if (m != null)
            {
                MainWindow w = m.Controls["main_window"] as MainWindow;
                if (w != null)
                {
                    w.ScreenGrid_show(w.LobbyScreen);
                }
            }            
        }

        void Connection_OnNetworkData(object sender, tcpconnection.OnNetworkDataEvent e)
        {
            messages.message.context c = new messages.message.context();
            c.Connection = sender as connection;
            c.IPEndPoint = e.IPEndPoint;
            e.Message.process(c);
        }

        public void sendmessage(messages.message message)
        {
            if (m_connection != null)
            {
                m_connection.sendmessage(message);
            }
        }
    }
}
